<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area" ref="areaHook1">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list g-clearfix">
          <div class="button-wrapper">
            <div class="button">{{this.city}}</div>
          </div>
        </div>
      </div>
      <div class="area" ref="areaHook2">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list g-clearfix">
          <div class="button-wrapper" v-for="item in hotCities" :key="item.id">
            <div class="button" @click="handleCityClick(item.name)">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div v-if="JSON.stringify(cities) !== '{}'">
        <div
          class="area areaHook"
          v-for="(item,key) of cities"
          :key="key"
          ref="areaHook"
        >
          <div class="title border-topbottom">{{key}}</div>
          <div class="item-list" v-for="innerItem of item" :key="innerItem.id">
            <div class="item border-bottom" @click="handleCityClick(innerItem.name)">{{innerItem.name}}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script type='text/babel'>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  props: {
    cities: Object,
    hotCities: Array,
    letter: Number
  },
  data () {
    return {
      scrollY: 0,
      listHeight: []
    }
  },
  created () {},
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper, {
      probeType: 3
    })
    this.scroll.on('scroll', (pos) => {
      this.scrollY = Math.abs(Math.round(pos.y))
      this.$emit('changeIndex', this.currentIndex)
    })
  },
  updated (e) {
    if (!this.$refs.areaHook1) {
      return
    }
    let areaHookH1, areaHookH2
    areaHookH1 = this.$refs.areaHook1.clientHeight
    areaHookH2 = this.$refs.areaHook2.clientHeight
    let $areaHook
    /**
     * 左侧list 区块高度递增数组
     * */
    let height = 0
    this.listHeight.push(height)
    $areaHook = this.$refs.areaHook
    for (let i = 0; i < $areaHook.length; i++) {
      height += $areaHook[i].clientHeight
      this.listHeight.push(height)
    }
    let h
    h = this.listHeight[1] + areaHookH1 + areaHookH2
    this.listHeight.splice(1, 1, h)
  },
  methods: {
    handleCityClick (city) {
      // this.$store.commit('changeCity', city)
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
  },
  computed: {
    currentIndex () {
      for (let i = 0; i < this.listHeight.length; i++) {
        let height1, height2
        height1 = this.listHeight[i]
        height2 = this.listHeight[i + 1]
        if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
          return i
        }
      }
      return 0
    },
    ...mapState(['city'])
  },
  watch: {
    letter () {
      this.scroll.scrollToElement(this.$refs.areaHook[this.letter])
    }
  },
  components: {}
}
</script>
<style scoped lang="scss" rel="stylesheet/scss">
  .border-topbottom{
    &:before,&:after{
      border-color: #ccc;
    }
  }
  .list{
    position: absolute;
    top:1.68rem;
    bottom: 0;
    left:0;
    right:0;
    overflow: hidden;
    .title{
      line-height:.44rem;
      background: #eee;
      padding-left: .2rem;
      color: #666;
      font-size:14px;
    }
    .button-list{
      padding: .1rem .6rem .1rem .1rem;
      .button-wrapper{
        float: left;
        width:33.33%;
        .button{
          padding: .1rem 0;
          margin: .1rem;
          text-align: center;
          border:.02rem solid #ccc;
          border-radius: .02rem;
        }
      }
    }
    .item-list{
      .item{
        line-height:.76rem;
        padding-left:.1rem;
      }
      .border-bottom{
        &:before{
          border-color: #ccc;
        }
      }
    }
  }
</style>
